package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.NavUtils;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.utils.ToastUtil;

public class SearchProjectCountFragment extends Fragment implements OnCheckedChangeListener {
  public static final String EXTRA_CONTENT =
      "com.isunland.managesystem.entity.SearchProjectCountFragment.EXTRA_CONTENT";
  private CheckBox mCbPmName;
  private CheckBox mCbCustomer;
  private CheckBox mCbProjectKind;
  private CheckBox mCbProjectStages;
  private CheckBox mCbFinishTime;
  private CheckBox mCbSignTime;
  private CheckBox mCbIsCheck;
  private ProjectCountContent queryContent;

  @Override public void onCreate(Bundle savedInstanceState) {
    // 设置返回图标
    setHasOptionsMenu(true);
    if (NavUtils.getParentActivityName(getActivity()) != null) {
      ((BaseVolleyActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    ((BaseVolleyActivity) getActivity()).getSupportActionBar().setTitle(R.string.query);
    //	    queryColsStr=(String) getArguments().getSerializable(EXTRA_CONTENT);

    queryContent = (ProjectCountContent) getArguments().getSerializable(EXTRA_CONTENT);
    super.onCreate(savedInstanceState);
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_confirm, menu);
    super.onCreateOptionsMenu(menu, inflater);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      // 响应返回图标
      case android.R.id.home:

        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }
        break;

      case R.id.menu_item_confirm:

        if (TextUtils.isEmpty(queryContent.getPmName())
            && TextUtils.isEmpty(queryContent.getProjectKindName())
            && TextUtils.isEmpty(queryContent.getPartaName())
            && TextUtils.isEmpty(queryContent.getStages())
            && TextUtils.isEmpty(queryContent.getRequireDate())
            && TextUtils.isEmpty(queryContent.getProjectSignDate())
            && TextUtils.isEmpty(queryContent.getIfAcceptance())) {
          ToastUtil.show(R.string.atLeastChooseOne);
        } else {

          Intent i = new Intent();
          i.putExtra("queryContent", queryContent);
          getActivity().setResult(Activity.RESULT_OK, i);
          getActivity().finish();
          //			LogUtil.i("queryColsStr====="+str.toString());

        }
        break;

      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_search_project_count, container, false);
    initData(v);

    return v;
  }

  private void initData(View v) {
    mCbPmName = (CheckBox) v.findViewById(R.id.cb_pmName);
    mCbCustomer = (CheckBox) v.findViewById(R.id.cb_customer);
    mCbProjectKind = (CheckBox) v.findViewById(R.id.cb_projectKind);
    mCbProjectStages = (CheckBox) v.findViewById(R.id.cb_projectStages);
    mCbFinishTime = (CheckBox) v.findViewById(R.id.cb_finishTime);
    mCbSignTime = (CheckBox) v.findViewById(R.id.cb_signTime);
    mCbIsCheck = (CheckBox) v.findViewById(R.id.cb_isCheck);

    mCbPmName.setOnCheckedChangeListener(this);
    mCbCustomer.setOnCheckedChangeListener(this);
    mCbProjectKind.setOnCheckedChangeListener(this);
    mCbProjectStages.setOnCheckedChangeListener(this);
    mCbFinishTime.setOnCheckedChangeListener(this);
    mCbSignTime.setOnCheckedChangeListener(this);
    mCbIsCheck.setOnCheckedChangeListener(this);

    if (!TextUtils.isEmpty(queryContent.getPmName())) {
      mCbPmName.setChecked(true);
    } else {
      mCbPmName.setChecked(false);
    }
    if (!TextUtils.isEmpty(queryContent.getPartaName())) {
      mCbCustomer.setChecked(true);
    } else {
      mCbCustomer.setChecked(false);
    }
    if (!TextUtils.isEmpty(queryContent.getProjectKindName())) {
      mCbProjectKind.setChecked(true);
    } else {
      mCbProjectKind.setChecked(false);
    }
    if (!TextUtils.isEmpty(queryContent.getStages())) {
      mCbProjectStages.setChecked(true);
    } else {
      mCbProjectStages.setChecked(false);
    }
    if (!TextUtils.isEmpty(queryContent.getRequireDate())) {
      mCbFinishTime.setChecked(true);
    } else {
      mCbFinishTime.setChecked(false);
    }
    if (!TextUtils.isEmpty(queryContent.getProjectSignDate())) {
      mCbSignTime.setChecked(true);
    } else {
      mCbSignTime.setChecked(false);
    }
    if (!TextUtils.isEmpty(queryContent.getIfAcceptance())) {
      mCbIsCheck.setChecked(true);
    } else {
      mCbIsCheck.setChecked(false);
    }
  }

  @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    switch (buttonView.getId()) {

      case R.id.cb_projectKind:
        if (isChecked) {
          queryContent.setProjectKindName("PROJECT_KIND_NAME");
        } else {
          queryContent.setProjectKindName("");
        }

        break;
      case R.id.cb_projectStages:
        if (isChecked) {
          queryContent.setStages("STAGES");
        } else {
          queryContent.setStages("");
        }
        break;
      case R.id.cb_pmName:
        if (isChecked) {
          queryContent.setPmName("pm_name");
        } else {
          queryContent.setPmName("");
        }

        break;
      case R.id.cb_customer:
        if (isChecked) {
          queryContent.setPartaName("PARTA_NAME");
        } else {
          queryContent.setPartaName("");
        }

        break;

      case R.id.cb_finishTime:
        if (isChecked) {
          queryContent.setRequireDate("require_date");
        } else {
          queryContent.setRequireDate("");
        }
        break;
      case R.id.cb_signTime:
        if (isChecked) {
          queryContent.setProjectSignDate("PROJECT_SIGN_DATE");
        } else {
          queryContent.setProjectSignDate("");
        }
        break;
      case R.id.cb_isCheck:
        if (isChecked) {
          queryContent.setIfAcceptance("if_acceptance");
        } else {
          queryContent.setIfAcceptance("");
        }
        break;

      default:
        break;
    }
  }

  public static SearchProjectCountFragment newInstence(ProjectCountContent queryContent) {
    Bundle bundle = new Bundle();
    bundle.putSerializable(EXTRA_CONTENT, queryContent);
    SearchProjectCountFragment mfragment = new SearchProjectCountFragment();
    mfragment.setArguments(bundle);
    return mfragment;
  }
}
